/**************************************************************************
 *         Copyright(c) 2007 by Sunplus mMedia  Technology Co., Ltd.       *
 *                                                                         *
 *  This software is copyrighted by and is the property of Sunplus mMedia  *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno- *
 *  logy Co., Ltd. This software may only be used in accordance with the   *
 *  corresponding license agreement. Any unauthorized use, duplication,    *
 *  distribution, or disclosure of this software is expressly forbidden.   *
 *                                                                         *
 *  This Copyright notice "M U S T" not be removed or modified without     *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.          *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this  *
 *  software without notice.                                               *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd.                                    *
 *  19-1, Innovation First Road, Science-Based Industrial Park,            *
 *  Hsin-Chu, Taiwan, R.O.C.                                               *
 *                                                                         *
 **************************************************************************/
/**
 * @file		app_battery.c
 * @brief		battery detect releated api
 * @author	Phil Lin
 * @since		2008-07-01
 * @date		2008-07-01
 */

/**************************************************************************
 *                            H E A D E R   F I L E S
 **************************************************************************/
#include "general.h"
#include "key_scan.h"
#include "gpio.h"
#include "app_ui_power.h"
#include "app_battery.h"

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
 
/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/
 /*Define the gap value of battery detect*/
#define BATT_DETECT_DEV	10
/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
/*
Note: Customer define battery adc value here ,
When the host receive the msg "SP1K_MSG_BAT_LV_UPDATE", 
you can get the battery level by "appBatLvGet"
*/

static UINT16 xdata battAdcVal[] = {
	POWER_BAT_LV_FULL,
	POWER_BAT_LV_HALF,
	POWER_BAT_LV_LOW,
	POWER_BAT_LV_EMPTY,
};

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/


/**************************************************************************
 *               F U N C T I O N    D E C L A R A T I O N S               *
 **************************************************************************/

//-----------------------------------------------------------------------------
//appBattInit
//-----------------------------------------------------------------------------
/**
 * @brief		appBattInit
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-07-01
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/

void appBattInit(void)
{
	UINT8 battLv;

	/*Battery detect configure*/	
	battDetectCfg(sizeof(battAdcVal) / sizeof(UINT16), &battAdcVal[0], BATT_DETECT_DEV);

	/*Battery detect initio, The value returned is the current battery level*/
	battLv = battDetectInit();

	/*Enable battery detect function*/
	sp1kBattEnable(1);

	/*Disabled battery detect in power on process on EVB hw solution*/
#if (_HW_SOLUTION_ != _HW_EVB_)
	if (sp1kUSB_PlugGet() == USB_PLUG_IN) {
		/*If power on with the USB power, disable the battery detect function*/
		sp1kBattEnable(DISABLE);
	} else {
		if (battLv == POWER_BAT_BELOWEMPTY) {
			/*Low battery detected in power on process*/
			printf("Low battery!\n");
			appPowerShutDown();
		}
	}
#endif
}
